using System;

using Microsoft.SPOT;

using mfRCF.Core;
using mfRCF.Core.Extensions;

using mfXmlSerializer;

using mfLogger;

using OST.Shared.SharedTypes;
using OST.Persistence;
using OST.Shared;
using System.Text;

namespace OST.Commands
{
    using Extensions;
    using System.IO;

    /// <summary>
    /// saves/overwrites the actual xml-serialized-Configuration and persists it
    /// </summary>
    public class SetConfigurationGETCommand : mfRcfCommand
    {
        #region CTOR

        public SetConfigurationGETCommand(string httpPath, Object targetOfInvocation)
            : base(httpPath, new mfGETMethod())
        {
            if (targetOfInvocation == null)
                throw new OSTException("targetOfInvocation is null");

            TargetOfInvocation = targetOfInvocation;
        }

        #endregion

        #region Properties

        /// <summary>
        /// wrapper-property 4 logging
        /// </summary>
        private ILoggerAppender Logger
        {
            get
            {
                return mfLogger.mfLogger.Instance;
            }
        }

        public Object TargetOfInvocation { get; set; }

        #endregion

        #region mfRcfCommand
        public override void ExecuteCommand(mfIHttpContext httpContext)
        {
            try
            {
                if (!RemoveableMediaStorageProvider.Instance.IsStorageConntected)
                    throw new OSTException("no storage connected");

                var configSerializer = new mfXmlTypedSerializer(TargetOfInvocation.GetType());

                this.ApplyConfigChanges(httpContext, configSerializer, TargetOfInvocation);
            }
            catch (Exception ex)
            {
                this.WriteHttpResponseError(httpContext, ex);
            }
        }
        #endregion
    }
}
